﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Domain;
using LX.Core.Utils;
using LX.Core.Cryptography;
using LX.Web.Utils;
using S4Store.Service.Valid.AdministratorValid;

namespace S4Store.Service
{
    public class AdministratorService : BaseService
    {
        public S4Store.Service.Valid.AdministratorValid.Add AddValidExp { get; set; }
        public S4Store.Service.Valid.AdministratorValid.Modify ModifyValidExp { get; set; }

        public IQueryable<Administrator> GetQuery()
        {
            return this.GetQuery<Administrator>();
        }

        public Administrator GetAdministrator(int id)
        {
            return GetQuery().FirstOrDefault(t => t.Id == id);
        }

        public void AddAdministrator(Administrator model)
        {
            this.Valid(model, AddValidExp);
            model.Password = MD5.Encrypt(model.Password);
            if (model.PositionType == Position.GeneralManager.ToInt()||
                model.PositionType == Position.SystemManager.ToInt())
            {
                model.StoreId = null;
            }
            this.GetSet<Administrator>().Add(model);
        }

        public void ModifyAdministrator(Administrator model)
        {
            Administrator old = GetQuery().First(t => t.Id == model.Id);
            old.StoreId = model.StoreId;
            old.PositionType = model.PositionType;
            old.Name = model.Name;
            if (model.Password != "******" && model.Password.Length < 32)
            {
                old.Password = MD5.Encrypt(model.Password);
            }
            if (old.PositionType == Position.GeneralManager.ToInt() ||
                model.PositionType == Position.SystemManager.ToInt())
            {
                old.StoreId = null;
            }
            this.Valid(old, ModifyValidExp);
        }

        public void DeleteAdministrator(int id)
        {
            Administrator model = GetQuery().First(t => t.Id == id);
            if (model == null) return;
            this.GetSet<Administrator>().Remove(model);
        }

        public Administrator Login(String username, String password)
        {
            if (Helper.StringIsEmpty(username))throw new Exception("请填写用户名");
            if (Helper.StringIsEmpty(password))throw new Exception("请填写密码");
            var model = this.GetQuery<Administrator>().First(t => t.UserName == username.Trim());
            if (model == null) throw new Exception("用户名或密码错误");
            if (model.Password != MD5.Encrypt(password)) throw new Exception("用户名或密码错误");
            if (model.IsClose) throw new Exception("帐号已关闭");   
            return model;
        }

        public Administrator GetLoginUser()
        {
            String ticket = HttpHelper.GetCookie("TICKET");
            int id;
            if (int.TryParse(ticket, out id))
            {
                Administrator administrator = GetAdministrator(id);
                return administrator;
            }
            return null;
        }

        public bool IsLogined()
        {
            String ticket= HttpHelper.GetCookie("TICKET");
            return !Helper.StringIsEmpty(ticket);
        }

        public void Logout()
        {
            HttpHelper.WriteCookie("TICKET", "",-1);
        }

        public void SetTicket(Administrator model)
        {
            HttpHelper.WriteCookie("TICKET", model.Id.ToString());
        }
    }
}
